import time

from selenium import webdriver
from selenium.common import exceptions
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service

print('\n@Copyright CAIYOUPENG\n@Copyright GeekChunky\n@Copyright GeekCC')
time.sleep(1)
print('\n林科大涉外实训平台自动化刷课程序V2.0\n基于Python3 + Selenium4构建\n')
time.sleep(1)


def adjustVerify():
    try:
        driver.implicitly_wait(2)
        driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/div[2]/div[1]/div[4]/div/div/div[2]/div[1]/a').click()
    except exceptions.NoSuchElementException:
        print('\n' + str(exceptions) + '\n')
        driver.find_element(By.PARTIAL_LINK_TEXT, '确定').click()
        print('验证码错误！请看清楚了好好输入！')
        return withCheckIn()


def withCheckIn():
    verify = input('请输入登录验证码：（作者暂时没有水平破解高难度随机验证码）')
    driver.find_element(By.XPATH, '//*[@id="code"]').send_keys(verify)
    driver.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[5]/div/input[2]').click()
    print('\n等待系统进一步指示...请稍后...')
    adjustVerify()


# 账号信息
account = '20207598'
password = 'Hxh20011214'

# 登录链接（戴希实训）
Login_Url = 'https://swxykdsx.daixiwangluo.com/user/login'

# 选择浏览器
# driver = webdriver.Safari()
# driver = webdriver.Edge(r'/Users/caiyoupeng00/Desktop/msedgedriver')
edge_Options = webdriver.EdgeOptions()
edge_Options.add_argument("--mute-audio")
s = Service()
# s = Service(r'/Users/caiyoupeng00/Desktop/msedgedriver')
driver = webdriver.Edge(service=s, options=edge_Options)

# 设置窗体大小
driver.set_window_size(height=600, width=400)

# 导向登录链接
driver.get(Login_Url)

# 输入账号密码，勾选保持登录
driver.find_element(By.XPATH, '//*[@id="username"]').send_keys(account)
driver.find_element(By.XPATH, '//*[@id="password"]').send_keys(password)
driver.find_element(By.XPATH, '//*[@id="remember"]').click()

withCheckIn()
print('已就绪...\n如需中途退出仅关闭自动化程序即可')

# 打开网课
driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/div/div[1]/div[2]/div[6]/div[1]/a').click()

# 设置窗体大小
driver.set_window_size(height=1366, width=768)

# 获取当前网课的链接
latest_video = driver.current_url


# 网课循环函数
def circulation():
    global latest_video

    # 获取网课名称文本信息
    video_message = latest_video.replace('https://swxykdsx.daixiwangluo.com', '')

    time.sleep(1)
    driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/div/div[2]/div[2]/div[2]/div/div/div[9]/canvas').click()
    # driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/div/div[2]/div[2]/div[2]/div/div/div[2]/div[1]/canvas').click()

    print('\n开始自动化播放视频，方便起见已为您最小化视频窗口，并将所有网课视频静音')
    driver.minimize_window()

    display_time = driver.find_element(By.XPATH,
                                       '/html/body/div[3]/div[2]/div/div[2]/div[2]/div[2]/div/div/div[2]/div[8]').text

    # TODO：是不是通过跳转网页的方式无法生效

    # 添加评论
    driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/div/div[2]/div[2]/div[3]/div[3]/form/div/div[2]/div[2]/textarea').send_keys('学到了，6')
    driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/div/div[2]/div[2]/div[3]/div[3]/form/div/div[2]/div[1]/div[2]/input').click()
    time.sleep(2)
    print('已为您自动评论...')

    lst_videoTime = list(display_time.replace('00:00 / ', ''))
    video_minute = int(lst_videoTime[0] + lst_videoTime[1])
    video_second = int(lst_videoTime[3] + lst_videoTime[4])

    # 输出控制台
    print('\n当前页面网课为：' + driver.find_element(By.XPATH, f'//a[@href="{video_message}"]').text)
    print('视频总时长为：' + str(video_minute) + '分' + str(video_second) + '秒')
    print('\n将在' + str(video_minute) + '分' + str(video_second + 5) + '秒后自动为您跳转视频')

    # 等待下一步介入时长
    time.sleep(video_minute * 60 + video_second + 5)

    print('\n\t正在跳转下一网课...')

    # 更新下一个网课
    # latest_video = last_video[:-1] + str(int(last_video[-1]) + 1)
    video_model = 'https://swxykdsx.daixiwangluo.com/user/node?nodeId='
    video_id = int(latest_video.replace('https://swxykdsx.daixiwangluo.com/user/node?nodeId=', '')) + 1
    latest_video = video_model + str(video_id)
    driver.get(latest_video)
    driver.implicitly_wait(10)


try:
    i = 1
    while True:
        circulation()
        print('已经成功学习' + str(i) + '个视频！')
        i = i + 1
except exceptions:
    print(exceptions)
    pass
print("自动化操作出现异常，请检查网课是否完成")
driver.maximize_window()
time.sleep(10000)
